<template>
  <el-dialog
    :title="$t('shop.accountManagement')"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="700px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      :label-width="this.$i18n.t('language') === 'language' ? '160px' : '100px'"
      style="width:500px;"
    >
      <el-form-item :label="$t('prodList.shopName')" prop="shopName" style="width:500px;">
        <el-input v-model="dataForm.shopName" disabled></el-input>
      </el-form-item>
      <br />
      <el-form-item :label="$t('shop.shopAccount')" prop="mobile" style="width:580px;">
        <el-row style="width:100%">
          <el-col :span="20">
            <el-input
              v-model="dataForm.mobile"
              maxlength="50"
              show-word-limit
              :placeholder="$t('shop.addShopErrorTip3')"
              :disabled="!editMobile"
            ></el-input>
          </el-col>
          <el-col :span="4">
            <el-button
              icon="el-icon-edit"
              type="text"
              size="small"
              v-if="!editMobile"
              @click="changeEditMobile()"
            >{{$t('platform.modifyAcc')}}</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <br />
      <el-form-item :label="$t('shop.loginPassword')" prop="password" style="width:500px;">
        <el-input type="password" v-model="dataForm.password" maxlength="50" show-word-limit></el-input>
      </el-form-item>
      <br />
      <el-form-item :label="$t('sys.confirmPassword')" prop="checkPassword" style="width:500px;">
        <el-input type="password" v-model="dataForm.checkPassword" maxlength="50" show-word-limit></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('remindPop.cancel')}}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{$t('remindPop.confirm')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import ImgsUpload from '@/components/imgs-upload'
export default {
  data () {
    var validateMobile = (rule, value, callback) => {
      var reg = /[^\d]/
      if (reg.test(value)) {
        callback(new Error(this.$i18n.t('shop.addShopErrorTip5')))
      } else {
        this.$http({
          url: this.$http.adornUrl('/shop/shopAuditing/checkMobile'),
          method: 'get',
          params: this.$http.adornParams({
            mobile: this.dataForm.mobile,
            shopId: this.dataForm.shopId
          })
        }).then(({ data }) => {
          if (data) {
            callback()
          } else {
            callback(new Error(this.$i18n.t('shop.addShopErrorTip6')))
          }
        })
      }
    }
    var validatePassword = (rule, value, callback) => {
      var reg = /[^\w]/
      if (reg.test(value)) {
        callback(new Error(this.$i18n.t('shop.addShopErrorTip7')))
      } else {
        callback()
      }
    }
    var validateCheckPassword = (rule, value, callback) => {
      var reg = /[^\w]/
      if (reg.test(value)) {
        callback(new Error(this.$i18n.t('shop.addShopErrorTip7')))
      } else if (this.dataForm.password !== this.dataForm.checkPassword) {
        callback(new Error(this.$i18n.t('shop.notEmptyTip10')))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      editMobile: false,
      dataForm: {
        shopId: 0,
        mobile: '',
        password: '',
        checkPassword: ''
      },
      dataRule: {
        mobile: [
          { required: true, message: this.$i18n.t('shop.errorTip11'), trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$i18n.t('sys.passwordNoNull'), trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: this.$i18n.t('sys.confirmPassNoNull'), trigger: 'blur' },
          { validator: validateCheckPassword, trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    ImgsUpload
  },
  methods: {
    init (shopId) {
      this.dataForm.shopId = shopId || 0
      this.visible = true
      this.show = false
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$refs.dataForm.resetFields()
        this.editMobile = false
        this.dataForm.mobile = ''
        this.dataForm.password = ''
        this.dataForm.checkPassword = ''
        this.getDataInfo()
      })
    },
    changeEditMobile () {
      this.editMobile = true
    },
    // 获取店铺详情数据
    getDataInfo () {
      this.$http({
        url: this.$http.adornUrl('/shop/shopAuditing/shopDetail/' + this.dataForm.shopId),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.dataForm = data
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let mobile = this.editMobile ? this.dataForm.mobile : null
          this.$http({
            url: this.$http.adornUrl('/shop/shopAuditing/updatePasswordOrMobile'),
            method: 'put',
            data: this.$http.adornData({
              shopId: this.dataForm.shopId,
              password: this.dataForm.password,
              mobile: mobile
            })
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    },
    message (msg) {
      this.$message({
        message: msg,
        type: 'error',
        duration: 1500
      })
    }
  }
}
</script>

